# Makefile for building BPF prefetcher and user-space applications
# Compiler and tools
CC = gcc
CXX = g++
CLANG = clang
BPFTOOL = bpftool

# Output file names
BPF_OBJ = prefetcher.bpf.o
BPF_SKEL = prefetcher.skel.h
USER_APP = prefetcher
TEST_APP = test_app
# --- 新增 ---
TEST_APP_MT = test_app_multithread

# CFLAGS for user-space program
# LDFLAGS for linking libraries
# We need to explicitly link against libbpf and libelf
CFLAGS = -g -Wall
LDFLAGS = -lbpf -lelf

# BPF compilation flags
# Tells clang to compile for the bpf target architecture
BPF_CFLAGS = -g -O2 -target bpf -D__TARGET_ARCH_x86

# Default target
# --- 修改 ---
all: $(USER_APP) $(TEST_APP) $(TEST_APP_MT)

# --- BPF Program Compilation ---

# Rule to compile BPF C source to BPF object file
$(BPF_OBJ): prefetcher.bpf.c prefetcher.h vmlinux.h
	$(CLANG) $(BPF_CFLAGS) -c $(filter %.c,$^) -o $@

# Rule to generate BPF skeleton header from the BPF object file
# This rule depends on the BPF object file being created first.
$(BPF_SKEL): $(BPF_OBJ)
	$(BPFTOOL) gen skeleton $< > $@

# --- User-space Program Compilation ---

# Rule to build the user-space loader
# It depends on the C++ source and the generated skeleton header.
$(USER_APP): prefetcher.cc $(BPF_SKEL)
	$(CXX) $(CFLAGS) -o $@ $< $(LDFLAGS)

# Rule to build the test application
$(TEST_APP): test_app.c
	$(CC) $(CFLAGS) -o $@ $<

# --- 新增：编译多线程测试程序 ---
$(TEST_APP_MT): test_app_multithread.c
	$(CC) $(CFLAGS) -o $@ $< -lpthread

# --- Clean Rule ---

clean:
	rm -f $(USER_APP) $(TEST_APP) $(TEST_APP_MT) $(BPF_SKEL) $(BPF_OBJ) *.log test_file.dat